
//当前时间
nowTime();
setInterval(() => {
    nowTime();
}, 1000);
//当前时间
function nowTime() {
    $("#time").text(moment().format("YYYY-MM-D HH:mm:ss"));
}

//数据库状态
var dataBaseState = false
//更新数据库状态
function updateDataBaseState(state) {
    if (state == 1) {
        dataBaseState = true;
    }
}

//检查数据库状态
function checkDataBaseState() {
    if (dataBaseState == false) {
        message("数据库连接失败，无法操作", 2);
    }
    return dataBaseState;
}

//检查开始采集状态
function checkStartState() {
    if (startCollectState == true) {
        message("已开始采集，无法操作", 2);
    }
    return startCollectState;
}

///更新项目信息
function updateProjectInfo(number, name) {
    var text = "当前工程：" + name
    $("#projectBtn").text(text);
}

//更新阶段
function updateStage(id, msg) {
    if (msg == '--') {
        $(id).addClass('layui-btn-disabled')
        $(id + "Row").addClass('disable');
    }
    else if (msg == '合格') {
        $(id).addClass('ok')
    }
    $(id).text(msg);
}

//loading
var loadIndex;
function loading(msg) {
    loadIndex = layer.msg(msg, { icon: 16, shade: 0.3, time: 0 });
}
//loading结束
function endLoading() {
    layer.close(loadIndex);
}

//消息
function message(msg, icon) {
    var a = arguments[0] ? arguments[0] : '';
    var b = arguments[1] ? arguments[1] : 7;
    if (a.length > 0) {
        layer.msg(msg, { icon: b, shade: 0.3, time: 900 });
    }
}

//压力表盘配置
function pressureChartOptions(type) {
    var option = gaugeChartOptions();
    if (type == "main") {
        option['series'][0]['data'] = [{ "value": 0, "name": "近端压力" }]
    } else if (type == "remote") {
        option['series'][0]['data'] = [{ "value": 0, "name": "远端压力" }]
    }

    option['series'][0]['detail']['formatter'] = '{value} Mpa'
    option['series'][0]['detail']['color'] = "#16b777"
    option['series'][0]['itemStyle']['color'] = "#16b777"
    return option;
}

//仪表板配置
function gaugeChartOptions() {
    var option = {
        series: [
            {
                type: 'gauge',
                center: ['46%', '70%'],
                startAngle: 180,
                endAngle: 0,
                min: 0,
                max: 30,
                itemStyle: {
                    color: '#FFAB91'
                },
                progress: {
                    show: true,
                    width: 15
                },
                pointer: {
                    show: false
                },
                axisLine: {
                    lineStyle: {
                        width: 15,
                    }
                },
                axisTick: {
                    distance: -45,
                    splitNumber: 5,
                    lineStyle: {
                        width: 2,
                        color: '#999'
                    }
                },
                splitLine: {
                    distance: -52,
                    length: 14,
                    lineStyle: {
                        width: 3,
                        color: '#999'
                    }
                },
                axisLabel: {
                    distance: -20,
                    color: '#999',
                    fontSize: 14
                },
                anchor: {
                    show: false
                },
                title: {
                    show: true
                },
                detail: {
                    valueAnimation: true,
                    width: '20%',
                    lineHeight: 20,
                    borderRadius: 8,
                    offsetCenter: [0, '-15%'],
                    fontSize: 30,
                    fontWeight: 'bolder',
                    formatter: '{value} °C',
                    color: 'inherit',
                    textStyle: {
                        fontSize: 14
                    }
                },
                data: [
                    {
                        value: 0,
                        name: '温度'
                    }
                ]
            }
        ]
    };
    return option;
}

function lineChartOption() {
    var now = moment(moment().format("YYYY-MM-DD HH:mm") + ":00");
    var startDate = now.format("YYYY-MM-DD HH:mm:ss");
    var endDate = now.add(5, 'm').format("YYYY-MM-DD HH:mm:ss");
    var option = {
        tooltip: {
            trigger: "axis",
        },
        toolbox: {
            show: true,
            right: "10%",
            feature: {
                myRealtime: { // 必须以my开头,不然不会显示
                    show: true,
                    title: '实时',
                    icon: 'path://M680.106667 774.165333c4.266667 0 7.936 1.536 11.029333 4.650667a15.061333 15.061333 0 0 1 4.650667 11.008 15.061333 15.061333 0 0 1-4.650667 11.029333l-33.557333 33.557334c11.605333 10.773333 24.746667 19.093333 39.445333 24.96a122.218667 122.218667 0 0 0 45.781333 8.832 123.221333 123.221333 0 0 0 61.226667-15.914667 123.477333 123.477333 0 0 0 45.546667-43.84c1.792-2.773333 6.122667-12.330667 12.992-28.650667 1.28-3.754667 3.754667-5.632 7.338666-5.632h47.018667c2.133333 0 3.968 0.768 5.504 2.325334a7.530667 7.530667 0 0 1 2.090667 7.210666c-10.453333 43.754667-32.32 79.232-65.621334 106.410667-33.322667 27.178667-72.32 40.789333-117.077333 40.789333a183.466667 183.466667 0 0 1-69.184-13.482666 186.922667 186.922667 0 0 1-59.626667-38.442667l-31.573333 31.573333a15.061333 15.061333 0 0 1-11.029333 4.672 15.061333 15.061333 0 0 1-11.029334-4.650666 15.061333 15.061333 0 0 1-4.650666-11.029334v-109.717333c0-4.245333 1.557333-7.893333 4.650666-11.008a15.061333 15.061333 0 0 1 11.029334-4.650667h109.696zM160 127.978667l0.128 493.76 205.845333-215.573334a32 32 0 0 1 43.584-2.496l2.474667 2.282667 100.501333 103.146667L830.698667 190.72a32 32 0 0 1 47.466666 42.794667l-2.197333 2.432-341.077333 341.333333a32 32 0 0 1-43.114667 1.962667l-2.432-2.24-100.010667-102.634667-229.184 240 0.042667 149.632h330.730667v64H138.88c-23.552 0-42.666667-19.114667-42.666667-42.666667l-0.085333-180.757333a32.192 32.192 0 0 1 0-5.824L96 128h64z m582.826667 426.752c23.829333 0 46.997333 4.522667 69.546666 13.589333a189.738667 189.738667 0 0 1 59.989334 38.336l31.829333-31.594667a15.061333 15.061333 0 0 1 11.029333-4.650666c4.245333 0 7.914667 1.536 11.008 4.650666a15.061333 15.061333 0 0 1 4.672 11.008v109.717334a15.061333 15.061333 0 0 1-4.672 11.029333 15.061333 15.061333 0 0 1-11.008 4.650667h-109.717333a15.061333 15.061333 0 0 1-11.029333-4.650667 15.061333 15.061333 0 0 1-4.650667-11.029333c0-4.245333 1.557333-7.914667 4.650667-11.008l33.813333-33.813334c-24.170667-22.357333-52.672-33.536-85.482667-33.536a123.221333 123.221333 0 0 0-61.226666 15.914667 123.477333 123.477333 0 0 0-45.546667 43.84c-1.792 2.773333-6.122667 12.309333-12.970667 28.650667-1.322667 3.754667-3.754667 5.632-7.36 5.632h-48.725333a7.530667 7.530667 0 0 1-5.504-2.346667 7.530667 7.530667 0 0 1-2.346667-5.504v-1.706667c10.624-43.733333 32.661333-79.232 66.133334-106.410666 33.472-27.178667 72.661333-40.768 117.546666-40.768z',
                    onclick: function () {
                        start = 50;
                    }
                },
                myAll: { // 必须以my开头,不然不会显示
                    show: true,
                    title: '全部',
                    icon: 'path://M170.666667 106.666667l0.192 736H906.666667v64H149.546667c-23.552 0-42.666667-19.093333-42.666667-42.666667L106.666667 106.666667h64z m686.506666 454.144l13.653334 16.362666a21.333333 21.333333 0 0 1-2.666667 30.058667l-171.157333 143.146667a21.333333 21.333333 0 0 1-21.546667 3.477333l-229.973333-91.285333-113.834667 94.997333a21.333333 21.333333 0 0 1-30.037333-2.709333l-13.653334-16.362667a21.333333 21.333333 0 0 1 2.688-30.058667l133.312-111.274666a21.333333 21.333333 0 0 1 21.546667-3.456l229.930667 91.264 151.68-126.826667a21.333333 21.333333 0 0 1 30.037333 2.666667z m-1.621333-417.962667l16.896 13.013333a21.333333 21.333333 0 0 1 3.925333 29.888L685.802667 433.706667a21.333333 21.333333 0 0 1-20.202667 8.085333l-226.794667-35.413333-150.186666 222.357333a21.333333 21.333333 0 0 1-27.477334 7.018667l-2.133333-1.28-17.685333-11.946667a21.333333 21.333333 0 0 1-5.738667-29.610667l165.354667-244.821333a21.333333 21.333333 0 0 1 20.992-9.130667L650.453333 374.613333l175.146667-227.882666a21.333333 21.333333 0 0 1 29.930667-3.904z',
                    onclick: function () {
                        start = 0;
                    }
                },
                saveAsImage: {
                    show: true,
                    type: 'png',
                    name: '下载',
                    title: '保存为图片',
                },
            },
        },
        dataZoom: [{
            type: 'slider',
            realtime: true,
            top: "410px"
        }],
        xAxis: {
            type: 'time',
            name: '时间',
            min: startDate,
            //max: endDate,
            axisLabel: {
                formatter: '{MM}-{dd} {HH}:{mm}:{ss}',
                show: true,
                showMinLabel: true,
                showMaxLabel: true,
                width: 60,
                overflow: 'break'
            }
        },
        yAxis: {
            min: 0,
            max: 30,
        },
        series: [
            {
                name: '近端介质温度',
                data: 0
            },
            {
                name: '温度',
                data: 0
            }
        ],
    };
    return option;
}

function dataURLtoBlob(dataurl) {
    var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
    while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
    }
    return new Blob([u8arr], { type: mime });
}
 
function downloadFile(url,name='What\'s the fuvk'){
    var a = document.createElement("a")
    a.setAttribute("href",url)
    a.setAttribute("download",name)
    a.setAttribute("target","_blank")
    let clickEvent = document.createEvent("MouseEvents");
    clickEvent.initEvent("click", true, true);  
    a.dispatchEvent(clickEvent);
}
 
function downloadFileByBase64(base64,name){
    var myBlob = dataURLtoBlob(base64)
    var myUrl = URL.createObjectURL(myBlob)
    downloadFile(myUrl,name)
}